import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {Http, Headers} from '@angular/http';
import {MatrixLogic} from '../entity/matrix_logic.model';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class MatrixLogicService {
  private readonly param = 'matrixLogics';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) {
  }

  /**
   * 获得矩阵逻辑
   *
   * @returns {Promise<MatrixState[]>}
   * @memberof MatrixStateService
   */
  getMatrixLogics(): Promise<MatrixLogic[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(response => response.data as MatrixLogic[])
      .catch(this.handleError);
  }

  /**
   * 根据矩阵逻辑分组ID获得矩阵逻辑
   *
   * @param {number} stateGroupid
   * @returns {Promise<MatrixState[]>}
   * @memberof MatrixLogicService
   */
  getMatrixLogicsByLogicGroupid(logicGroupId: number): Promise<MatrixLogic[]> {
    return this.http.get(`${this.url}/logicGroupId/${logicGroupId}`)
      .toPromise()
      .then(response => response.data as MatrixLogic[])
      .catch(this.handleError);
  }

  update(matrixLogic: MatrixLogic): Promise<MatrixLogic> {
    const url = `${this.url}`;
    return this.http.post(url, JSON.stringify(matrixLogic))
      .toPromise()
      .then(() => matrixLogic)
      .catch(this.handleError);
  }

  /**
   * 表达式验证
   *
   * @param {string} expression
   * @returns {Promise<any>}
   * @memberof MatrixLogicService
   */
  expressionCheck(expr: string): Promise<any> {
    const exprCheckUrl = `${AppConfig.APP_RESTFUL}/turbStatusExprs/expr`;
    return this.http.post(exprCheckUrl, JSON.stringify(expr))
      .toPromise()
      .then(res => {
        try {
          let result = res;
          return result;
          /* switch (result.code) {
            case "200":
              return result.data.expr;
            case "-1":
              // throw new Error(result.message);
              return result.data.expr;
          } */
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  //删除
  // delete(id: number): Promise<void> {
  //   const url = `${this.url}/${id}`;
  //   return this.http.delete(url, { headers: this.headers })
  //     .toPromise()
  //     .then(() => null)
  //     .catch(this.handleError);
  // }
  // //批量线路删除
  // batchDelete(keyIds: number[]): Promise<boolean> {
  //   const apiUrl = `${this.url}?items=${keyIds}`;
  //   return this.http.delete(apiUrl, { headers: this.headers })
  //     .toPromise()
  //     .then(response => {
  //       try {
  //         let result = response;
  //         switch (result.code) {
  //           case "200":
  //             return true;
  //           case "-1":
  //             throw new Error(result.message);
  //         }
  //       }
  //       catch (error) {
  //         throw error;
  //       }

  //     })
  //     .catch(this.handleError);
  // }

  private handleError(error: any): Promise<any> {
    // console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }


}


// WEBPACK FOOTER //
// ./src/app/service/matrix-logic.service.ts
